using System.Net;

namespace CollabApp.API.Common;

/// <summary>
/// 统一API响应格式
/// </summary>
/// <typeparam name="T">响应数据类型</typeparam>
public class ApiResponse<T>
{
    /// <summary>
    /// 是否成功
    /// </summary>
    public bool Success { get; set; }

    /// <summary>
    /// 响应消息
    /// </summary>
    public string Message { get; set; } = string.Empty;

    /// <summary>
    /// 响应数据
    /// </summary>
    public T? Data { get; set; }

    /// <summary>
    /// 错误列表
    /// </summary>
    public List<string>? Errors { get; set; }

    /// <summary>
    /// 时间戳
    /// </summary>
    public DateTime Timestamp { get; set; } = DateTime.UtcNow;

    /// <summary>
    /// HTTP状态码
    /// </summary>
    public int StatusCode { get; set; } = (int)HttpStatusCode.OK;

    /// <summary>
    /// 创建成功响应
    /// </summary>
    /// <param name="data">响应数据</param>
    /// <param name="message">响应消息</param>
    /// <returns>成功响应</returns>
    public static ApiResponse<T> CreateSuccess(T? data = default, string message = "操作成功")
    {
        return new ApiResponse<T>
        {
            Success = true,
            Message = message,
            Data = data,
            StatusCode = (int)HttpStatusCode.OK
        };
    }

    /// <summary>
    /// 创建成功响应（带消息列表）
    /// </summary>
    /// <param name="data">响应数据</param>
    /// <param name="messages">消息列表</param>
    /// <returns>成功响应</returns>
    public static ApiResponse<T> CreateSuccess(T? data, List<string> messages)
    {
        return new ApiResponse<T>
        {
            Success = true,
            Message = messages.FirstOrDefault() ?? "操作成功",
            Data = data,
            StatusCode = (int)HttpStatusCode.OK
        };
    }

    /// <summary>
    /// 创建失败响应
    /// </summary>
    /// <param name="message">错误消息</param>
    /// <param name="errors">错误列表</param>
    /// <param name="statusCode">HTTP状态码</param>
    /// <returns>失败响应</returns>
    public static ApiResponse<T> CreateFailure(string message, List<string>? errors = null, int statusCode = (int)HttpStatusCode.BadRequest)
    {
        return new ApiResponse<T>
        {
            Success = false,
            Message = message,
            Data = default,
            Errors = errors,
            StatusCode = statusCode
        };
    }

    /// <summary>
    /// 创建失败响应（单个错误）
    /// </summary>
    /// <param name="message">错误消息</param>
    /// <param name="error">错误信息</param>
    /// <param name="statusCode">HTTP状态码</param>
    /// <returns>失败响应</returns>
    public static ApiResponse<T> CreateFailure(string message, string error, int statusCode = (int)HttpStatusCode.BadRequest)
    {
        return CreateFailure(message, new List<string> { error }, statusCode);
    }

    /// <summary>
    /// 创建错误响应
    /// </summary>
    /// <param name="message">错误消息</param>
    /// <param name="errors">错误列表</param>
    /// <param name="statusCode">HTTP状态码</param>
    /// <returns>错误响应</returns>
    public static ApiResponse<T> CreateError(string message, List<string>? errors = null, int statusCode = (int)HttpStatusCode.InternalServerError)
    {
        return new ApiResponse<T>
        {
            Success = false,
            Message = message,
            Data = default,
            Errors = errors,
            StatusCode = statusCode
        };
    }

    /// <summary>
    /// 创建验证失败响应
    /// </summary>
    /// <param name="errors">验证错误列表</param>
    /// <returns>验证失败响应</returns>
    public static ApiResponse<T> CreateValidationError(Dictionary<string, List<string>> errors)
    {
        var errorList = errors.SelectMany(kvp => 
            kvp.Value.Select(v => $"{kvp.Key}: {v}")).ToList();

        return new ApiResponse<T>
        {
            Success = false,
            Message = "验证失败",
            Data = default,
            Errors = errorList,
            StatusCode = (int)HttpStatusCode.BadRequest
        };
    }

    /// <summary>
    /// 创建未找到响应
    /// </summary>
    /// <param name="message">消息</param>
    /// <returns>未找到响应</returns>
    public static ApiResponse<T> CreateNotFound(string message = "未找到资源")
    {
        return new ApiResponse<T>
        {
            Success = false,
            Message = message,
            Data = default,
            StatusCode = (int)HttpStatusCode.NotFound
        };
    }

    /// <summary>
    /// 创建未授权响应
    /// </summary>
    /// <param name="message">消息</param>
    /// <returns>未授权响应</returns>
    public static ApiResponse<T> CreateUnauthorized(string message = "未授权访问")
    {
        return new ApiResponse<T>
        {
            Success = false,
            Message = message,
            Data = default,
            StatusCode = (int)HttpStatusCode.Unauthorized
        };
    }

    /// <summary>
    /// 创建禁止访问响应
    /// </summary>
    /// <param name="message">消息</param>
    /// <returns>禁止访问响应</returns>
    public static ApiResponse<T> CreateForbidden(string message = "禁止访问")
    {
        return new ApiResponse<T>
        {
            Success = false,
            Message = message,
            Data = default,
            StatusCode = (int)HttpStatusCode.Forbidden
        };
    }
}